/*!
shared handle for multi-thread
- 维护配置文件和行情数据
- 使用std::sync::Rwlock, 而没有使用tokio::sync::RwLock, 因为虽然前者会导致阻塞，但是因为egui是同步的，所以更方便调用。对于程序应用场景而言，阻塞没有影响。

*/

use crate::*;
use anyhow::bail;
use chrono::Local;
use common::SubFutureInfo;
use serde::{Deserialize, Serialize};
use serde_json::*;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use sqlx::sqlite::SqlitePool;
use tokio::sync::{
    mpsc::{Receiver, Sender},
    Mutex,
};

/**
多线程共享数据句柄
*/

pub struct Shared {
    pub cfg_file: String,
    //pub server_uri: String,
    //pub site_root: String,
    pub dbpool: SqlitePool,       
    pub cfg: Config,
}

impl Shared {
    pub fn new(cfg_file: &str, dbpool: SqlitePool) -> Self {
        //let server_uri = server_uri.to_string();
        //let site_root = site_root.to_string();
        //let startup = Local::now().format("%Y%m%d %H:%M:%S").to_string();
        let src = String::from_utf8(std::fs::read(cfg_file).unwrap()).unwrap();
        let cfg: Config = serde_json::from_str(src.as_str()).unwrap();
        Self {
            cfg_file: cfg_file.to_string(),           
            cfg,
            dbpool,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
    pub futures: HashMap<String, Vec<SubFutureInfo>>,
    // pub site_root: String,
    // pub server_addr: String,
}

impl Config {
    pub fn new() -> Self {
        let futures: HashMap<String, Vec<SubFutureInfo>> = HashMap::new();
        let  site_root = "".to_string();
        Self { futures }
    }
    pub fn default() -> Self {
        let src = r#"{
    "futures": {
        "AP": [
            {
                "code": "AP501",
                "addr": "8.155.160.209:9995"
            },
            {
                "code": "AP505",
                "addr": "www.mark.lengsh.cn:9995"
            },
            {
                "code": "AP510",
                "addr": "8.155.160.209:9994"
            }
        ],
        "MA": [
            {
                "code": "MA501",
                "addr": "8.155.160.209:9995"
            },
            {
                "code": "MA505",
                "addr": "www.mark.lengsh.cn:9995"
            },
            {
                "code": "MA509",
                "addr": "8.155.160.209:9994"
            }
        ],
        "hc": [
            {
                "code": "hc2501",
                "addr": "8.155.160.209:9995"
            },
            {
                "code": "hc2505",
                "addr": "www.mark.lengsh.cn:9995"
            },
            {
                "code": "hc2509",
                "addr": "8.155.160.209:9994"
            }
        ],
        "rb": [
            {
                "code": "rb2501",
                "addr": "8.155.160.209:9995"
            },
            {
                "code": "rb2505",
                "addr": "www.mark.lengsh.cn:9995"
            },
            {
                "code": "rb2509",
                "addr": "8.155.160.209:9994"
            }
        ],
        "SR": [
            {
                "code": "SR501",
                "addr": "8.155.160.209:9995"
            },
            {
                "code": "SR505",
                "addr": "www.mark.lengsh.cn:9995"
            },
            {
                "code": "SR509",
                "addr": "8.155.160.209:9994"
            }
        ],
        "SA": [
            {
                "code": "SA501",
                "addr": "8.155.160.209:9995"
            },
            {
                "code": "SA505",
                "addr": "www.mark.lengsh.cn:9995"
            },
            {
                "code": "SA509",
                "addr": "8.155.160.209:9994"
            }
        ],
        "jm": [
            {
                "code": "jm2501",
                "addr": "8.155.160.209:9995"
            },
            {
                "code": "jm2505",
                "addr": "www.mark.lengsh.cn:9995"
            },
            {
                "code": "jm2509",
                "addr": "8.155.160.209:9994"
            }
        ]
    }
}"#;
        let futures: HashMap<String, Vec<SubFutureInfo>> = serde_json::from_str(src).unwrap();
        // let  site_root = "./".to_string();
        Self { futures }
    }
}
